/**
 * Created by leo on 2/2.
 */
angular.module('panels')
    .directive('panelItilToDoTaskListPersonal', function ($http, tabService, LoginInfo, loadConf, $timeout, $log, Promise) {
        return {
            link: function (scope) {
                Promise.all({
                    setting: loadConf('panel-itil-to-do-task-list-personal.yml'),
                    loginInfo: LoginInfo
                }).then(function (result) {
                    var setting = result.setting, loginInfo = result.loginInfo, timeoutId;
                    scope.more = setting.more;
                    scope.$on("reload-panel",function(e,data){
                        if(data=='itil-to-do-task-list-personal'){
                            $log.debug('panel-update data: itil-to-do-task-list-personal');
                            $http.jsonp(setting.listQueryUrl.replace('{userId}', loginInfo.currentUser.LOGIN_ID).replace('{T}',new Date().getTime())).success(function (data) {
                                scope.rows = data;
                            })
                        }
                    });
                    if (setting.processList) {
                        scope.todoList = setting.processList;
                    }
                    scope.$on('$destroy', function () {
                        timeoutId && $timeout.cancel(timeoutId);
                    });
                    (function updateData() {
                        $http.jsonp(setting.listQueryUrl.replace('{userId}', loginInfo.currentUser.LOGIN_ID).replace('{T}',new Date().getTime())).success(function (data) {
                            scope.rows = data;
                        }).then(callback, callback);
                        function callback() {
                            if (setting.updateInterval > 0) {
                                timeoutId = $timeout(updateData, setting.updateInterval * 1000);
                            }
                        }

                        $log.debug('panel-update data: itil-to-do-task-list-personal');
                    }());

                    scope.createTabFromIframe = function (r) {
                        var url =setting.dealUrl.replace('{SERIALNO}',r.ISERIALNO)
                            .replace('{TASKID}',r.ITASKID)
                            .replace('{IPRETASKID}',r.IPRETASKID)
                            .replace('{ISTAFFID}',r.ISTAFFID)
                            .replace('{INSTANCEID}',r.INSTANCEID)
                            .replace('{FROMINSTANCEID}',r.FROMINSTANCEID)
                            .replace('{PROCESSID}',r.IPROCESSID)
                            .replace('{ACTIVITYID}',r.ICURRACTID)
                            .replace('{ITASKSTATUS}',r.ITASKSTATUS);
                        tabService.addTab({
                            name : "个人工单处理",
                            url :url
                        });
                    }
                });
            },
            templateUrl: 'app/panel/itil/to-do-task-list-personal.html'
        };
    });